# debate_controller_backup.py
from agents import Host, Judge, Debater
from debate_recorder import DebateRecorder
import json
from debater_prompt import DebaterPrompt
from llm_integration import LLMIntegration


class DebateFlow:
    def __init__(self, flow_file):
        with open(flow_file, 'r', encoding='utf-8') as f:
            self.flow = json.load(f)

    def get_flow_stages(self):
        return self.flow.get('stages', [])


class DebateController:
    def __init__(self, topic):
        self.recorder = DebateRecorder()
        self.llm_integration = LLMIntegration(url="127.0.0.1:11434", type="normal", title="knowledge_base")
        self.host = Host("主持人", self.recorder)
        self.judge = Judge("裁判员", self.recorder, llm=self.llm_integration)
        self.topic = topic
        self.debaters = []
        chn_num = ["一", "二", "三", "四"]
        for num in chn_num:
            self.debaters.append(Debater(f"{num}辩", "正方", position=f"{num}辩", recorder=self.recorder,
                                         topic=self.topic, llm=self.llm_integration))
            self.debaters.append(Debater(f"{num}辩", "反方", position=f"{num}辩", recorder=self.recorder,
                                         topic=self.topic, llm=self.llm_integration))

        self.prompt_manager = DebaterPrompt("debater_prompts.json")
        # 给辩手赋值提示词
        for debater in self.debaters:
            prompt = self.prompt_manager.get_prompt(debater.side, debater.name)
            if prompt:
                debater.prompt = prompt

        self.debate_flow = DebateFlow("debate_flow.json")

    def get_debates_info(self):
        con_size = []
        pro_size = []
        for debater in self.debaters:
            if debater.side == "正方":
                pro_size.append([debater.side + debater.name, "蓄势代发"])
            else:
                con_size.append([debater.side + debater.name, "蓄势代发"])
        return pro_size, con_size

    def run_debate(self):
        """运行辩论赛流程"""
        # 开始阶段
        yield self.host.name, self.host.start_debate(topic=self.topic)

        # 辩论阶段
        stages = self.debate_flow.get_flow_stages()
        for stage in stages:
            stage_name = stage.get('name')
            yield self.host.name, self.host.control_stage(stage_name)
            actions = stage.get('actions', [])
            for action in actions:
                side = action.get('side')
                position = action.get('position')
                action_type = action.get('action_type')
                for debater in self.debaters:
                    if debater.side == side and debater.position == position:
                        yield debater.side+debater.name, debater.debate(stage_name, action_type)
                        break

        # 评审阶段
        yield self.host.name, self.host.statr_judge()
        yield self.judge.name, self.judge.summarize()
        yield self.host.name, self.host.end_debate()

if __name__ == '__main__':
    # 初始化控制器
    controller = DebateController("中学生是否应该带手机进入校园")

    # 运行辩论流程并打印输出
    print("===== 辩论开始 =====")
    for speaker, content in controller.run_debate():
        print(f"[{speaker}] {content}")
        print("-" * 50)
    print("===== 辩论结束 =====")